﻿using System;
using System.Windows.Data;

namespace Falafel
{
    public class NotBlankNumberFromTextConverter : IValueConverter

    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (!(value is string))
            {
                return value;
            }

            string str = (string)value;

            if (str.Trim().Length == 0)
            {
                return 0;
            }

            int rslt;
            if (!int.TryParse(str, out rslt))
            {
                return 0;
            }

            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
